package com.test31_IO应用;

import com.test31_IO应用.exception.ArrayLengthInvalidateException;
import com.test31_IO应用.util.InputUtil;

import java.util.Arrays;

interface IStringService {
    /** 数据的输入 */
    public void input();
    /** 数据的返回 */
    public String value();
}

class StringServiceImpl implements IStringService {
    private static final int DEFAULT_LENGTH = 3;
    private String[] data;

    public StringServiceImpl() {
        this(DEFAULT_LENGTH);
    }

    public StringServiceImpl(int len) {
        if (len <= 0) {
            throw new ArrayLengthInvalidateException("当前开辟的长度错误。");
        } else {
            this.data = new String[len];
        }
        this.input();   // 键盘输入数据
        this.reverse(); // 逆序处理
    }

    @Override
    public void input() {
        for (int x = 0; x < this.data.length; x++) {
            this.data[x] = InputUtil.getString("请输入第" + (x+1) + "个字符串：");
        }
    }

    /** 逆序处理 */
    private void reverse() {
        int center = this.data.length / 2;
        int head = 0;
        int tail = this.data.length - 1;
        for (int x = 0; x < center; x++) {
            String temp = this.data[head];
            this.data[head] = this.data[tail];
            this.data[tail] = temp;
            head++;
            tail--;
        }
    }

    @Override
    public String value() {
        return Arrays.toString(this.data);
    }
}

class StringFactory {
    private StringFactory() {
    }

    public static IStringService getInstance(int... args) {
        if (args.length == 1) {
            return new StringServiceImpl(args[0]);
        }
        return new StringServiceImpl();
    }
}

/**
 * 字符串逆序处理
 */
public class YootkDemo_02 {

    public static void main(String[] args) {
        IStringService stringService = StringFactory.getInstance();
        System.out.println(stringService.value());
    }
}
